package cn.harry.cabinet.viewmodel

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import cn.harry.cabinet.model.entity.Card
import cn.harry.cabinet.repository.CardRepository
import kotlinx.coroutines.launch

/**
 * 卡片ViewModel
 * 连接UI和数据仓库
 */
class CardViewModel(application: Application) : BaseViewModel(application) {
    
    private val repository = CardRepository(application)
    
    private val _cards = MutableLiveData<List<Card>>()
    val cards: LiveData<List<Card>> = _cards
    
    private val _totalCount = MutableLiveData<Int>()
    val totalCount: LiveData<Int> = _totalCount
    
    /**
     * 搜索卡片
     */
    fun searchCards(query: String) {
        showLoading()
        viewModelScope.launch {
            try {
                val result = if (query.isBlank()) {
                    repository.getAll()
                } else {
                    repository.search(query)
                }
                _cards.value = result
                _totalCount.value = repository.count()
                hideLoading()
            } catch (e: Exception) {
                hideLoading()
                showError("搜索卡片失败: ${e.message}")
            }
        }
    }
    
    /**
     * 刷新卡片列表
     */
    fun refreshCards() {
        showLoading()
        viewModelScope.launch {
            try {
                val result = repository.getAll()
                _cards.value = result
                _totalCount.value = repository.count()
                hideLoading()
            } catch (e: Exception) {
                hideLoading()
                showError("刷新卡片列表失败: ${e.message}")
            }
        }
    }
    
    /**
     * 添加新卡片
     */
    fun addCard(card: Card, callback: (() -> Unit)? = null) {
        showLoading()
        viewModelScope.launch {
            try {
                repository.insert(card)
                refreshCards()
                hideLoading()
                callback?.invoke()
            } catch (e: Exception) {
                hideLoading()
                showError("添加卡片失败: ${e.message}")
            }
        }
    }
    
    /**
     * 更新卡片
     */
    fun updateCard(card: Card, callback: (() -> Unit)? = null) {
        showLoading()
        viewModelScope.launch {
            try {
                repository.update(card)
                refreshCards()
                hideLoading()
                callback?.invoke()
            } catch (e: Exception) {
                hideLoading()
                showError("更新卡片失败: ${e.message}")
            }
        }
    }
    
    /**
     * 删除卡片
     */
    fun deleteCard(card: Card, callback: (() -> Unit)? = null) {
        showLoading()
        viewModelScope.launch {
            try {
                repository.delete(card)
                refreshCards()
                hideLoading()
                callback?.invoke()
            } catch (e: Exception) {
                hideLoading()
                showError("删除卡片失败: ${e.message}")
            }
        }
    }
    
    /**
     * 更新卡片状态
     */
    fun updateCardStatus(cardId: Long, status: String, callback: (() -> Unit)? = null) {
        showLoading()
        viewModelScope.launch {
            try {
                repository.updateStatus(cardId, status)
                refreshCards()
                hideLoading()
                callback?.invoke()
            } catch (e: Exception) {
                hideLoading()
                showError("更新卡片状态失败: ${e.message}")
            }
        }
    }
}
